home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / dosio_sprintf.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  3KB  |  132 lines

  1. RCS_ID_C="$Id: dosio_sprintf.c,v 4.2 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      dosio_sprintf.c - formatted print to a buffer using RawDoFmt()
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <sys/cdefs.h>        /* REG() and ASM macros */
  11. #include <dos/rdargs.h>        /* CSource */
  12. #include <stdarg.h>
  13.  
  14. #if __GNUC__
  15. #include <inline/exec.h>
  16. #elif __SASC
  17. #include <proto/exec.h>
  18. #else
  19. #include <clib/exec_protos.h>
  20. #endif
  21.  
  22. static void ASM
  23. __stuffchar(REG(d0) char ch,
  24.         REG(a3) struct CSource * sc)
  25. {
  26.   if (sc->CS_CurChr < sc->CS_Length 
  27.       && (sc->CS_Buffer[sc->CS_CurChr] = ch))
  28.     sc->CS_CurChr++;
  29. }
  30.  
  31. /****** net.lib/SPrintf ******************************************
  32.  
  33.     NAME    
  34.     SPrintf -- formatted print to a buffer
  35.  
  36.     SYNOPSIS
  37.     len = SPrintf(Buffer, FormatString, Arguments...)
  38.     len = VSPrintf(Buffer, FormatString, ap)
  39.  
  40.     ULONG SPrintf(STRPTR, const char *, ...)
  41.     ULONG VSPrintf(STRPTR, const char *,  va_list)
  42.  
  43.     FUNCTION
  44.     Prints to a simple buffer or to a CSource buffer. These functions
  45.     are similar to C-library sprintf() with RawDoFmt() formatting.
  46.  
  47.     INPUTS
  48.     Buffer - Pointer to buffer.
  49.     FormatString - This is a printf()-style format string as defined
  50.         in exec.library/RawDoFmt().
  51.     Arguments - as in printf() .
  52.  
  53.     Result - Pointer to CSource structure.
  54.  
  55.     RESULT
  56.     Number of characters printed.
  57.  
  58.     EXAMPLE
  59.         SPrintf(mybuf, "line=%ld, val=%lx\n", 
  60.         __LINE__, very.interesting->value);
  61.  
  62.     BUGS
  63.     Function SPrintf() assumes that no print is longer than 1024 chars.
  64.     It does not check for buffer owerflow (there no way to check, the
  65.     definition of sprintf misses it).
  66.  
  67.     SPrintf strings are truncated to maximum of 1024 chars (including
  68.     final NUL)
  69.  
  70.     SEE ALSO
  71.     exec.library/RawDoFmt()
  72.  
  73. ******************************************************************************
  74. *
  75. */
  76.  
  77. ULONG
  78. VCSPrintf(struct CSource *buf, const char *fmt, va_list ap)
  79. {
  80.   ULONG start = buf->CS_CurChr;
  81.  
  82.   if (buf->CS_Length && buf->CS_CurChr < buf->CS_Length) {
  83.     RawDoFmt((STRPTR)fmt, ap, __stuffchar, buf);
  84.     
  85.     if (buf->CS_CurChr == buf->CS_Length) {
  86.       buf->CS_CurChr--;            /* must NUL terminate */
  87.     }      
  88.     buf->CS_Buffer[buf->CS_CurChr] = '\0';
  89.  
  90.     return buf->CS_CurChr - start;
  91.   } else {
  92.     /* A pathological case */
  93.     return 0;
  94.   }
  95. }
  96.  
  97. ULONG
  98. CSPrintf(struct CSource *buf, const char *fmt, ...)
  99. {
  100.   va_list ap;
  101.   ULONG len;
  102.  
  103.   va_start(ap, fmt);
  104.   len = VCSPrintf(buf, fmt, ap);
  105.   va_end(ap);
  106.   return len;
  107. }
  108.  
  109. ULONG
  110. VSPrintf(char *buf, const char *fmt, va_list ap)
  111. {
  112.   struct CSource cs;
  113.  
  114.   cs.CS_Buffer = buf;
  115.   cs.CS_CurChr = 0;
  116.   cs.CS_Length = 1024;      /* This is not probably the cleanest way :-) */
  117.  
  118.   return VCSPrintf(&cs, fmt, ap);
  119. }
  120.  
  121. ULONG
  122. SPrintf(char *buf, const char *fmt, ...)
  123. {
  124.   va_list ap;
  125.   ULONG len;
  126.  
  127.   va_start(ap, fmt);
  128.   len = VSPrintf(buf, fmt, ap);
  129.   va_end(ap);
  130.   return len;
  131. }
  132.